/*
   Copyright (c) 2023 李伟国
   shiro-learner is licensed under Mulan PSL v2.
   You can use this software according to the terms and conditions of the Mulan PSL v2. 
   You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
   THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
   See the Mulan PSL v2 for more details. 
*/

package cn.learner.controller;

import cn.learner.config.response.ErrorResponseData;
import cn.learner.config.response.ResponseData;
import cn.learner.config.response.SuccessResponseData;
import cn.learner.controller.base.BaseController;
import cn.learner.entity.User;
import cn.learner.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping(value = "/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/info/{userId}", method = RequestMethod.GET)
    public ResponseData userInfo(@PathVariable("userId") Long userId){
        return new SuccessResponseData(userService.findUserInfo(userId));
    }

    @RequestMapping(value = "/info",method = RequestMethod.GET)
    public ResponseData userInfo(HttpServletRequest request){
        return new SuccessResponseData(userService.findUserLoginInfo(request));
    }

    @RequestMapping(value = "/menu", method = RequestMethod.GET)
    public ResponseData userMenu(){
        return SUCCESS_INFO;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseData userList(){
        return new SuccessResponseData(userService.findAll());
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseData userUpdate(@RequestBody User user){
        return new SuccessResponseData(userService.updateUser(user));
    }
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseData userDelete(@RequestBody User user){
        return new SuccessResponseData(userService.deleteUser(user));
    }
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public ResponseData enableUser(@RequestBody User user){
        return new SuccessResponseData(userService.enableUser(user));
    }
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseData insertUser(@RequestBody User user){
        if(user == null){
            return new ErrorResponseData();
        }
        return new SuccessResponseData(userService.insert(user));
    }
}
